/*
 * Copyright © 2009 Gerald Isaac Schwarz
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package base;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class BoardModifier extends JDialog {
	private JLabel widthLabel, heightLabel;
	private JSpinner widthOption, heightOption;
	private JButton apply, cancel;
	private GridBagConstraints constraints;
	private boolean result;
	private Board board;
	
	private ActionListener buttonListener = new ActionListener() {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals(apply.getActionCommand())) {
				setResult(true);
				setVisible(false);
			}
			else if (e.getActionCommand().equals(cancel.getActionCommand())) {
				setVisible(false);
			}
		}
		
	};
	
	private ChangeListener sizeListener = new ChangeListener() {

		@Override
		public void stateChanged(ChangeEvent e) {
			int width = (Integer) widthOption.getValue();
			int height = (Integer) heightOption.getValue();
			getBoard().setSize(new Dimension(width, height));
		}
		
	};
	
	@Override
	public void setVisible(boolean visible) {
		if (visible) {
			this.setResult(false);
		}
		
		super.setVisible(visible);
	}
	
	public static Board showDialog(Window window, String title, Board board) {
		BoardModifier boardModifier = new BoardModifier(window, title, board);
		
		boardModifier.setVisible(true);
		
		if (boardModifier.isResult()) {
			return boardModifier.getBoard();
		}
		else {
			return null;
		}
	}
	
	public BoardModifier(Window window, String title, Board board) {
		super(window, title, JDialog.DEFAULT_MODALITY_TYPE);
		
		this.widthLabel = new JLabel("Width");
		this.heightLabel = new JLabel("Height");
		
		this.widthOption = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
		this.heightOption = new JSpinner(new SpinnerNumberModel(1, 1, 100, 1));
		
		this.setBoard(board);
		
		this.widthOption.addChangeListener(this.sizeListener);
		this.heightOption.addChangeListener(this.sizeListener);
		
		this.widthLabel.setLabelFor(this.widthOption);
		this.heightLabel.setLabelFor(this.heightOption);
		
		this.cancel = new JButton("Cancel");
		this.apply = new JButton("Apply");
		
		this.cancel.setActionCommand("Cancel");
		this.apply.setActionCommand("Apply");
		
		this.cancel.addActionListener(this.buttonListener);
		this.apply.addActionListener(this.buttonListener);
		
		this.getContentPane().setLayout(new GridBagLayout());
		
		this.constraints = new GridBagConstraints();
		this.constraints.insets = new Insets(5, 5, 5, 5);
		this.constraints.anchor = GridBagConstraints.LINE_END;
		
		this.constraints.gridx = 0;
		this.constraints.gridy = 0;
		this.constraints.gridwidth = 1;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.widthLabel, this.constraints);
		
		this.constraints.gridx = 1;
		this.constraints.gridy = 0;
		this.constraints.gridwidth = 2;
		this.constraints.fill = GridBagConstraints.HORIZONTAL;
		this.getContentPane().add(this.widthOption, this.constraints);
		
		this.constraints.gridx = 0;
		this.constraints.gridy = 1;
		this.constraints.gridwidth = 1;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.heightLabel, this.constraints);
		
		this.constraints.gridx = 1;
		this.constraints.gridy = 1;
		this.constraints.gridwidth = 2;
		this.constraints.fill = GridBagConstraints.HORIZONTAL;
		this.getContentPane().add(this.heightOption, this.constraints);
		
		this.constraints.gridx = 1;
		this.constraints.gridy = 2;
		this.constraints.gridwidth = 1;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.cancel, this.constraints);
		
		this.constraints.gridx = 2;
		this.constraints.gridy = 2;
		this.constraints.gridwidth = 1;
		this.constraints.fill = GridBagConstraints.NONE;
		this.getContentPane().add(this.apply, this.constraints);
		
		this.cancel.setMnemonic(KeyEvent.VK_C);
		this.apply.setMnemonic(KeyEvent.VK_A);
		
		this.setResizable(false);
		this.pack();
	}

	private void setResult(boolean result) {
		this.result = result;
	}

	public boolean isResult() {
		return result;
	}

	private void setBoard(Board board) {
		this.board = board;
		
		Dimension size = this.getBoard().getSize();
		this.widthOption.setValue(size.width);
		this.heightOption.setValue(size.height);
	}

	public Board getBoard() {
		return board;
	}
}
